﻿using ConfExpert.Forms.Base;
using ConfExpert.Utility;
using DevExpress.XtraCharts;
using DevExpress.XtraPivotGrid;
using DevTeam;

using System;
using System.Data;

namespace ConfExpert.Forms.Depo
{
    public partial class Trends : XForm
    {
        public Trends()
        {
            InitializeComponent();
        }

        //http://documentation.devexpress.com/#corelibraries/clsDevExpressXtraPivotGridPivotGridOptionsChartDataSourcetopic
        //http://www.devexpress.com/Support/Center/Example/Details/E2214

        private void Trends_Load(object sender, EventArgs e)
        {
            //GRANDTOTAL gizle
            
            DataSet ds = ExecuteDataSet("GetTrendsA", dateEdit1.EditValue.ToDate());
            //InitPivot(pivotGridControl1, ds.Tables[0]);
            gridView1.Init(ds.Tables[0]);
            gridView1.MakeColumnInvisible("Name");
            gridView1.MakeReadOnly();

            //pivotGridControl1.OptionsChartDataSource.ProvideColumnFieldValuesAsType = typeof(DateTime);
            //pivotGridControl1.OptionsChartDataSource.ProvideCellValuesAsType = typeof(int);

            //chartControl1.DataSource = pivotGridControl1;
            //chartControl1.SeriesDataMember = "Series";
            //chartControl1.SeriesTemplate.ArgumentDataMember = "Arguments";
            //chartControl1.SeriesTemplate.ValueDataMembers.AddRange(new string[] { "Values" });
            //chartControl1.CustomDrawAxisLabel += chartControl1_CustomDrawAxisLabel;
            //chartControl1.BoundDataChanged += chartControl1_BoundDataChanged;

            ////chartControl1.PivotGridDataSourceOptions.RetrieveRowGrandTotals =
            ////chartControl1.PivotGridDataSourceOptions.RetrieveColumnGrandTotals =
            ////chartControl1.PivotGridDataSourceOptions.RetrieveRowTotals = false;

            //pivotGridControl1.OptionsChartDataSource.ProvideDataByColumns = false;
            //pivotGridControl1.CustomChartDataSourceData += pivotGridControl1_CustomChartDataSourceData;

            dateEdit1.EditValueChanged += dateEdit1_EditValueChanged;
            dateEdit1.EditValue = DateTime.Today;

            gridView1.FormatAsNumber(0, "RaftakiMiktar");
            gridView1.AddSummary("RaftakiMiktar");
            gridView1.BestFitColumns();

            //comboBoxEdit1.SelectedIndexChanged += comboBoxEdit1_SelectedIndexChanged;
            //comboBoxEdit1.Properties.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor;
            //checkButton1.CheckedChanged += checkButton1_CheckedChanged;
        }

        void dateEdit1_EditValueChanged(object sender, EventArgs e)
        {
            DataSet ds = ExecuteDataSet("GetTrendsA", dateEdit1.EditValue);
            //pivotGridControl1.DataSource = ds.Tables[0];
            gridControl1.DataSource = ds.Tables[0];
            gridView1.BestFitColumns();
        }

        //void chartControl1_BoundDataChanged(object sender, EventArgs e)
        //{
        //    SetDateFormat();
        //}

        void chartControl1_CustomDrawAxisLabel(object sender, CustomDrawAxisLabelEventArgs e)
        {
            if (e.Item.AxisValue.GetType() == typeof(DateTime))
                e.Item.Text = ((DateTime)e.Item.AxisValue).ToString("dd.MM.yyyy");
            else if (e.Item.AxisValue.GetType() == typeof(double))
                e.Item.Text = ((double)e.Item.AxisValue).ToString("#,###");
        }

        //private void SetDateFormat()
        //{
        //    (chartControl1.Diagram as XYDiagram).AxisX.DateTimeScaleOptions.MeasureUnit = DateTimeMeasureUnit.Day;
        //    (chartControl1.Diagram as XYDiagram).AxisX.DateTimeScaleOptions.GridAlignment = DateTimeGridAlignment.Day;
        //    (chartControl1.Diagram as XYDiagram).AxisX.Label.DateTimeOptions.Format = DateTimeFormat.ShortTime;
        //}



        #region #CustomChartDataSourceData
        private void pivotGridControl1_CustomChartDataSourceData(object sender, PivotCustomChartDataSourceDataEventArgs e)
        {
            if (e.ItemType == PivotChartItemType.RowItem)
            {
                if (e.FieldValueInfo.Field.FieldName == "CustomerId")
                {
                    if (intParse(e.FieldValueInfo.Value) > 0)
                        e.Value = AppSession.Lookups.V_Customer.FindByCustomerId((int)e.FieldValueInfo.Value).Name;
                }
            }
            //if (e.ItemType == PivotChartItemType.ColumnItem)
            //{
            //    if (e.FieldValueInfo.ValueType == PivotGridValueType.GrandTotal)
            //    {
            //        e.Value = "Total";
            //    }
            //}
            //if (e.ItemType == PivotChartItemType.CellItem)
            //{
            //    e.Value = Math.Round(Convert.ToDecimal(e.CellInfo.Value), 0);
            //}
        }
        #endregion #CustomChartDataSourceData


        void InitPivot(PivotGridControl pgc, DataTable dt)
        {
            pgc.DataSource = dt;

            pgc.OptionsCustomization.CustomizationFormStyle = DevExpress.XtraPivotGrid.Customization.CustomizationFormStyle.Excel2007;
            pgc.FieldValueDisplayText += new PivotFieldDisplayTextEventHandler(pivotGrid_FieldValueDisplayText);
            pgc.OptionsView.ShowColumnGrandTotals = false;

            pgc.BeginUpdate();

            //new field - toplam tutar  (metre x fiyat+dvz cinsi)

            /*
            PivotGridFieldBase[] pgf1 = new PivotGridFieldBase[3];
            pgf1[0] = AddNewField(pgc, "Linea", PivotArea.RowArea);
            pgf1[1] = AddNewField(pgc, "Modello", PivotArea.RowArea);
            pgf1[1].ExpandedInFieldsGroup = false;
            pgf1[2] = AddNewField(pgc, "Tex", PivotArea.RowArea);
            pgc.Groups.Add(pgf1, "ModelloTex");
            */

            //PivotGridField qa = AddNewField(pgc, "QtyAvailable", PivotArea.RowArea);
            //qa.ValueFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
            //qa.ValueFormat.FormatString = "#,###.0";
            //qa.Appearance.Value.Options.UseTextOptions = true;
            //qa.Appearance.Value.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Far;

            AddNewField(pgc, "CustomerId", PivotArea.RowArea).FieldEdit = RI(Rp.V_Customer);

            AddNewField(pgc, "Tarih", PivotArea.ColumnArea);
            //pivotGridControl1.Fields["Tarih"].ValueFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
            //pivotGridControl1.Fields["Tarih"].ValueFormat.FormatString = "d";

            /*
            PivotGridField[] fields2 = new PivotGridField[3];

            PivotGridField fieldYear = new PivotGridField("Tarih", PivotArea.ColumnArea);
            PivotGridField fieldMonth = new PivotGridField("Tarih", PivotArea.ColumnArea);
            PivotGridField fieldDay = new PivotGridField("Tarih", PivotArea.ColumnArea);

            // Add the fields to the field collection.
            pgc.Fields.Add(fieldYear);
            fieldYear.GroupInterval = PivotGroupInterval.DateYear;
            fieldYear.Caption = "Yıl";

            pgc.Fields.Add(fieldMonth);
            fieldMonth.GroupInterval = PivotGroupInterval.DateMonth;
            fieldMonth.Caption = "Ay";

            pgc.Fields.Add(fieldDay);
            fieldDay.GroupInterval = PivotGroupInterval.DateDay;
            fieldDay.Caption = "Gün";

            fields2[0] = fieldYear;
            fields2[1] = fieldMonth;
            fields2[2] = fieldDay;

            fields2[2].ExpandedInFieldsGroup = false;
            pgc.Groups.Add(fields2, "Tarih");
            */
            PivotGridField qa = AddNewField(pgc, "Qty", PivotArea.DataArea);
            qa.ValueFormat.FormatType = DevExpress.Utils.FormatType.Numeric;
            qa.ValueFormat.FormatString = "#,###.0";

            //AddNewField(pgc, "Rezerve", PivotArea.DataArea);
            //AddNewField(pgc, "Ayrilan", PivotArea.DataArea);
            //AddNewField(pgc, "Idle", PivotArea.DataArea);

            pgc.EndUpdate();

            pgc.BestFit();
        }

    }
}
